<template>
	<div>
		<el-form :model="comFormData" ref="comForm" :rules="Rule" label-width="150px" element-loading-text="正在提交数据...">
			<el-row>
				<el-col :span="8">
					<el-form-item label="车主姓名：" prop="insured">
						<el-input size="small" :maxlength="200" v-model="comFormData.insured" placeholder="请输入车主姓名">
							<el-select v-model="comFormData.source" style="width: 80px;" slot="prepend">
								<el-option label="个人" value="0"></el-option>
								<el-option label="单位" value="1"></el-option>
							</el-select>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="车主身份证：" prop="insuredIdNumber">
						<el-input size="small" :maxlength="200" v-model="comFormData.insuredIdNumber" placeholder="请输入车主身份证号"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="8">
					<el-form-item label="车主地址：" prop="insuredAddress">
						<el-input size="small" :maxlength="200" v-model="comFormData.insuredAddress" placeholder="请输入车主地址"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="8">
					<el-form-item label="车主邮箱：" prop="plateNum">
						<el-input size="small" :maxlength="200" v-model="comFormData.email" placeholder="请输入车主邮箱"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="8">
					<el-form-item label="车型：" prop="carModel">
						<!--<el-input size="small" v-model="comFormData.carModel"></el-input>-->
						<el-input placeholder="请选择车型" icon="search" v-model="comFormData.carModel" readonly :on-icon-click="handleIconClick">
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="车架号：" prop="vin">
						<el-input size="small" :maxlength="200" v-model="comFormData.vin" placeholder="请输入17位车架号"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="8">
					<el-form-item label="购买日期：" prop="firstRecordDate" required>
						<!--<el-input size="small" v-model="comFormData.firstRecordDate"></el-input>-->
						<el-date-picker size="small" style="width: 95%;" v-model="comFormData.firstRecordDate" type="date" placeholder="购买日期">
						</el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="车牌号：" prop="plateNum">
						<el-input size="small" :maxlength="200" v-model="comFormData.plateNum" placeholder="请输入车牌号"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">
					<el-form-item label="新车属性：">
						<el-select size="small" style="width: 100%;" v-model="comFormData.carProperty" placeholder="选择新车属性">
							<el-option label="新车金融" value="xcjr">
							</el-option>
							<el-option label="新车全款" value="xcqk">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="销售属性：">
						<el-select size="small" style="width: 100%;" v-model="comFormData.salesProperty" placeholder="选择销售属性">
							<el-option label="本地销" value="bdx">
							</el-option>
							<el-option label="外地销" value="wdx">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="8">
					<el-form-item label="联系人电话：" prop="contactPhone">
						<el-input size="small" style="width: 100%;" :maxlength="200" v-model="comFormData.contactPhone" placeholder="请输入联系人电话"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<el-row>
			<el-col :span="5" style="margin-left: 300px;">
				<el-button type="" v-on:click='reset' style="width: 120px;">重 置</el-button>
			</el-col>
			<el-col :span="5" style="">
				<el-button type="primary" v-on:click='save' style="width: 120px;">保 存</el-button>
			</el-col>
		</el-row>

		<el-dialog title="选择车型" v-model="showStart" size="small">
			<el-row :gutter="24">
				<el-col :span="11" style="margin-left:5px;">
					<el-input size="small" v-model="dictTreeSearchValue" placeholder="请输入内容" icon="search" @click="searchTree"></el-input>
				</el-col>
				<el-col :span="11" style="margin-left:5px;">
				</el-col>
				<el-col :span="24" style="margin-left:5px;">
					<div style="height: 3em;"></div>
				</el-col>
				<el-col :span="24" style="margin-left:5px; height: 250px;overflow: scroll;">
					<el-tree :data="paramTreeData" :filter-node-method="filterNode" :highlight-current=true :props="defaultProps" accordion @node-click="handleNodeClick" :auto-expand-parent=false ref="dictTree" node-key="id"></el-tree>
				</el-col>
			</el-row>

			<div slot="footer" class="dialog-footer">
				<el-button @click="showStart = false">取 消</el-button>
				<el-button type="primary" @click="saveCarSerices">提 交</el-button>
			</div>
		</el-dialog>

		<el-dialog title="选择要搜索的车型" v-model="searchCarShow" size="small">
			<el-row :gutter="24">
				<el-col :span="11" style="margin-left:5px;">
				</el-col>
				<el-col :span="24" style="margin-left:5px;">
					<div style="height: 3em;"></div>
				</el-col>
				<el-col :span="24" style="margin-left:5px; height: 250px;overflow: scroll;">
					<el-tree :data="SearchTreeData" :filter-node-method="filterNode" :highlight-current=true :props="defaultProps" accordion @node-click="searchNodeClick" :auto-expand-parent=false node-key="id"></el-tree>
				</el-col>
			</el-row>

			<el-dialog title="选择车型" v-model="showStart" size="small">
				<el-row :gutter="24">
					<el-col :span="11" style="margin-left:5px;">
						<el-input size="small" v-model="dictTreeSearchValue" placeholder="请输入内容" icon="search" @click="searchTree"></el-input>
					</el-col>
					<el-col :span="11" style="margin-left:5px;">
					</el-col>
					<el-col :span="24" style="margin-left:5px;">
						<div style="height: 3em;"></div>
					</el-col>
					<el-col :span="24" style="margin-left:5px; height: 250px;overflow: scroll;">
						<el-tree :data="paramTreeData" :filter-node-method="filterNode" :highlight-current=true :props="defaultProps" accordion @node-click="handleNodeClick" :auto-expand-parent=false ref="dictTree" node-key="id"></el-tree>
					</el-col>
				</el-row>

				<div slot="footer" class="dialog-footer">
					<el-button @click="showStart = false">取 消</el-button>
					<el-button type="primary" @click="saveCarSerices">提 交</el-button>
				</div>
			</el-dialog>

			<el-dialog title="选择要搜索的车型" v-model="searchCarShow" size="small">
				<el-row :gutter="24">
					<el-col :span="11" style="margin-left:5px;">
					</el-col>
					<el-col :span="24" style="margin-left:5px;">
						<div style="height: 3em;"></div>
					</el-col>
					<el-col :span="24" style="margin-left:5px; height: 250px;overflow: scroll;">
						<el-tree :data="SearchTreeData" :filter-node-method="filterNode" :highlight-current=true :props="defaultProps" accordion @node-click="searchNodeClick" :auto-expand-parent=false node-key="id"></el-tree>
					</el-col>
				</el-row>

				<div slot="footer" class="dialog-footer">
					<el-button @click="searchCarShow = false">取 消</el-button>
					<el-button type="primary" v-loading.fullscreen.lock="loading2" @click="serchSaveCar">提 交</el-button>
				</div>
			</el-dialog>

			<div slot="footer" class="dialog-footer">
				<el-button @click="searchCarShow = false">取 消</el-button>
				<el-button type="primary" @click="serchSaveCar">提 交</el-button>
			</div>
		</el-dialog>

	</div>
</template>
<script>
	import moment from 'moment'
	export default {
		data: function() {
			return {
				loading2: false,
				//firstRecordDate: null, //form里面的时间
				//保险单初始分页
				CurentPages: 1,
				CurentPage: 1,
				//每页的数量
				page: this.page,
				insuranceCombo: [{
					label: '交强险',
					value: '0'
				}, {
					label: '商业险',
					value: '1'
				}],
				commissionCombo: [{
					label: '常规佣金',
					value: '0'
				}, {
					label: '促销佣金',
					value: '1'
				}],
				//页码数
				CurentPage: 1,
				//每页的数量
				page: this.page,
				//公司数据
				tableData: {
					total: 0,
					dataList: []
				},
				//佣金数据
				//分组数据
				tableDatas: {
					total: 0,
					dataList: []
				},
				activeName: 'first',
				companyName: '',
				condition: {
					plateNum: "",
					carAge: "",
					carModel: "",
					insured: "",
					carValue: '',
					carLevel: '',
					vin: '',
				},
				conditions: {
					date: "",
					companyId: ""
				},
				comFormShow: false,
				comFormShows: false,
				//校验规则
				Rule: {
					contactPhone: [{
						required: true,
						message: '请输入联系人电话',
						trigger: 'blur'
					}],
					insured: [{
						required: true,
						message: '请输入车主姓名',
						trigger: 'blur'
					}],
					insuredIdNumber: [{
						required: true,
						message: '请输入身份证',
						trigger: 'blur'
					}],
					insuredAddress: [{
						required: true,
						message: '请输入车主地址',
						trigger: 'blur'
					}],

					vin: [{
						required: true,
						message: '请输入车架号',
						trigger: 'blur'
					}],
					email: [{
						required: true,
						message: '请输入email',
						trigger: 'blur'
					}],
					carModel: [{
						required: true,
						message: '请输入车型',
						trigger: 'blur'
					}],

				},
				//校验规则
				Rules: {},
				comFormData: {
					source:'0',//被保险人类型
					carProperty:'',//新车属性
					salesProperty:'',//销售属性
					insured: "",
					insuredIdNumber: "",
					vin: "",
					email: "",
					firstRecordDate: "",
					plateNum: "",
					insuredAddress: "",
					contactPhone: "",
					carLevel: '',
					carValue: '',

				},
				emptyData: {
					source:'0',//被保险人类型
					carProperty:'',//新车属性
					salesProperty:'',//销售属性
					insured: "",
					insuredIdNumber: "",
					vin: "",
					email: "",
					firstRecordDate: "",
					plateNum: "",
					insuredAddress: "",
					contactPhone: "",
					carLevel: '',
					carValue: '',
				},

				multipleSelection: [],
				multipleSelections: [],
				companyId: "",
				//树形图查询条件
				dictTreeSearchValue: "",
				paramTreeData: [],
				//搜索的车型数据
				SearchTreeData: [],
				//树型数据配置
				defaultProps: {
					children: 'child',
					label: 'label',
				},
				showStart: false,
				searchCarShow: false,
				//品牌
				company: "",
				//品牌
				companyData: [{
					label: '全部',
					value: 'all'
				}, {
					label: '大众',
					value: '2'
				}, {
					label: '别克',
					value: '3'
				}, ],
				//选中的类型（车型/车系）
				carType: '',
				//搜索的选中类型
				searhcarType: '',
				//选择的车辆信息
				datalabel: '',
				datavale: '',
				//搜索的车型
				searchcarType: '',
				searchdatalabel: '', //搜索的选择的车辆信息
				searchdatavale: '',
			}
		},
		methods: {
			//查询客户档案列表
			companyQuery: function() {
				this.$http.post(this.API_ROOT + '/insurance/car/page', this.$data.condition, {
					params: {
						start: this.$data.CurentPage,
						limit: this.page.pageSize
					}
				}).then(resp => {
					if(resp.data.success) {
						resp.data.total = parseInt(resp.data.total);
						// es5写法
						this.$data.tableData = resp.data;
						//this.baseQuery(this.$data.curTreeDeptId);
						//this.$data.exFormShow = false;
					} else {
						//debugger;
						this.$message.error(resp.data.msg);
					}
				}, resp => {});
			},
			//查询树节点
			searchTree: function() {
				if(this.$data.dictTreeSearchValue == "") {
					debugger;
					this.$message("请输入查询条件");
					this.queryParamTree();
					return;
				}
				this.$refs.dictTree.filter(this.$data.dictTreeSearchValue);
			},
			//点击资源树节点
			handleNodeClick(data) {
				debugger;
				this.$data.comFormData.carLevel = '';
				this.$data.carType = data.type;
				this.$data.comFormData.carLevel = data.type;
				this.$data.datalabel = data.label;
				this.$data.datavale = data.value;
			},
			//点击搜索的资源树节点
			searchNodeClick(data) {
				debugger;
				this.$data.condition.carLevel = '';
				this.$data.condition.carValue = '';

				this.$data.searhcarType = data.type;
				this.$data.searchdatalabel = data.label;
				this.$data.searchdatavale = data.value;
				this.$data.condition.carLevel = data.type;
				this.$data.condition.carValue = data.value;
			},
			//查询参数树
			queryParamTree: function() {
				//获取资源树
				debugger;
				this.$http.get(this.API_ROOT + '/base/car/tree', {}).then((response) => {
					this.$data.paramTreeData = response.data;
				}, (response) => {});
			},
			//查询搜索的参数树
			querySearchTree: function() {
				//获取资源树
				debugger;
				this.$http.get(this.API_ROOT + '/base/car/tree', {}).then((response) => {
					this.$data.SearchTreeData = response.data;
				}, (response) => {});
			},
			//资源过滤
			filterNode: function(value, data, node) {
				if(!value) return true;
				return data.label.indexOf(value) !== -1;
			},
			//手动触发
			//			baseQuery: function() {
			//				this.$data.showStart = true;
			//
			//			},
			//确认车型
			saveCarSerices: function() {
				debugger;
				this.$data.comFormData.carModel = '';
				if(this.$data.carType == '0') {
					this.$message("请不要选择品牌");
					
				} else {
					this.$data.comFormData.carModel = this.$data.datalabel;
					this.$data.comFormData.carValue = this.$data.datavale;
				}
				this.$data.showStart = false;
			},
			//搜索确认车型
			serchSaveCar: function() {
				debugger;
				this.$data.condition.carModel = '';
				if(this.$data.searhcarType == '2') {
					this.$data.condition.carModel = this.$data.searchdatalabel;
				} else {
					this.$message("未选中车型");
				}
				this.$data.searchCarShow = false;
			},
			//打开车型车系选择
			handleIconClick: function() {
				this.$data.showStart = true;
			},
			//打开搜索的车型选择
			chooseSearchModel: function() {
				this.$data.searchCarShow = true;
			},
			//状态转换
			statusCov: function(row, column) {
				return row.status == "1" ? '已启用' : '已停用';
			},
			//保险类别转换
			//状态转换
			insCov: function(row, column) {
				return row.insuranceType == "1" ? '商业险' : '交强险';
			},
			//状态转换
			commCov: function(row, column) {
				return row.commissionType == "1" ? '促销佣金' : '常规佣金';
			},
			//佣金类别转换

			//选择框
			handleSelectionChange: function(val) {
				debugger;
				this.$data.multipleSelection = val;
			},
			//选择框
			handleSelectionChanges: function(val) {
				debugger;
				this.$data.multipleSelections = val;
			},
			//切换tab页
			handleClick: function(tab, event) {
				console.log(tab, event);
			},

			reset: function() {
				//alert("置空");
				this.$data.comFormData.carModel = "";
				this.$data.comFormData = Object.assign(this.$data.comFormData, this.$data.emptyData);
			},

			//保存
			save: function() {
				this.$data.comFormData.firstRecordDate = this.momentFu(this.$data.comFormData.firstRecordDate);
				debugger;
				if(this.isCardNo(this.$data.comFormData.insuredIdNumber) == false) {
					this.$message.error("请填写正确的身份证号");
				} else if(this.$data.comFormData.vin.length != 17) {
					this.$message.error("请填写正确的车架号");
				} else if(this.$data.comFormData.plateNum == '') {
					this.$confirm('确定不填写车牌号吗?', '提示', { //出单询问
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						this.$refs.comForm.validate(function(valid, $event) {
							// 验证通过
							if(valid) {
								debugger;
								//alert("t1");
								this.$data.loading2 = true;
								this.$http.post(this.API_ROOT + '/insurance/car/save/add', this.$data.comFormData, {
									params: {}
								}).then(resp => {
									if(resp.data.success) {
										//alert("success");
										this.$message(resp.data.msg);
										this.reset();
									} else {
										this.$message.error(resp.data.msg);
									}
									this.$data.loading2 = false;
								}, resp => {});

							} else {
								//alert("f1");
								return false
							}
						}.bind(this))
					}).catch(() => {
						this.$message({
							type: 'info',
							message: '取消'
						});
					});
				} else {
					this.$refs.comForm.validate(function(valid, $event) {
						// 验证通过
						if(valid) {
							//alert("t2");
							this.$data.loading2 = true;
							this.$http.post(this.API_ROOT + '/insurance/car/save/add', this.$data.comFormData, {
								params: {}
							}).then(resp => {
								if(resp.data.success) {
									//alert("success");
									this.$message(resp.data.msg);
									this.reset();
								} else {
									this.$message.error(resp.data.msg);
								}
								this.$data.loading2 = false;
							}, resp => {});
						} else {
							//alert("f2");
							return false
						}
					}.bind(this))
				}
			},
			//身份验证
			isCardNo: function(card) {
				debugger;
				// 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
				var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
				if(reg.test(card) === false) {
					return false;
				} else {
					return true;
				}
			},
			//时间判断为空的封装
			momentFu: function(dt) {
				debugger;
				if(dt == '' || dt == null || dt == undefined) {
					// this.$message.error("请选择初次登记日期");
					return "";
				} else {
					return this.moment(dt).format("YYYY-MM-DD");
				}
			},
			//列表，分页大小切换
			handleSizeChange: function() {},
			//保险单列表分页
			handleSizeChanges: function() {},
			//保险单列表分页切换
			handleCurrentChanges: function(currentPages) {
				this.$data.currentPages = currentPages;
				this.info();
			},
			//列表,当前页切换
			handleCurrentChange: function(currentPage) {
				debugger;
				this.$data.CurentPage = currentPage;
				//this.$data.dictGroup = '';
				this.companyQuery();
			},

		},
		//表单创建时执行
		created: function() {
			this.companyQuery();
			this.queryParamTree();
			this.querySearchTree();
		}
	}
</script>
<style>
	.el-table .info-row {
		height: 10px !important;
	}
	
	.item {
		width: 30px;
	}
	
	.el-form-item {
		clear: both;
	}
	
	.el-dropdown .el-button-group,
	.el-table .el-tooltip,
	.el-table .el-tooltip__rel {
		display: inline;
	}
	
	.el-table {
		height: 100%;
		width: 100%;
	}
	
	.baoxian {
		white-space: normal !important;
	}
	
	#el-select {
		width: 200px;
	}
</style>